package com.wlyuan.open.scanner.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlyuan.open.scanner.dao.entity.OrderFlowDO;
import com.wlyuan.open.scanner.dao.mapper.OrderFlowMapper;
import com.wlyuan.open.scanner.repository.OrderFlowRepository;
import com.wlyuan.open.scanner.utils.StreamUtils;
import com.wlyuan.open.scanner.utils.enums.order.OrderStatusEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderFlowRepositoryImpl implements OrderFlowRepository {
    private final OrderFlowMapper orderflowMapper;

    @Override
    public OrderFlowDO getSignFlowByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
        //状态从待签收变为待回单
        wrapper.eq(OrderFlowDO::getFlowOrderid, orderId)
                .eq(OrderFlowDO::getFlowSrcstatus, OrderStatusEnum.WAIT_SIGN.getValue()).eq(OrderFlowDO::getFlowDststatus, OrderStatusEnum.WAIT_RECEIPT.getValue());
        var list = orderflowMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //根据订单id分组，取insertTime最大的
        return StreamUtils.max(list, Comparator.comparing(OrderFlowDO::getFlowInserttime));
    }

    @Override
    public OrderFlowDO getReceiptFlowByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
        //状态从待回单变为已回单
        wrapper.eq(OrderFlowDO::getFlowOrderid, orderId)
                .eq(OrderFlowDO::getFlowSrcstatus, OrderStatusEnum.WAIT_RECEIPT.getValue()).eq(OrderFlowDO::getFlowDststatus, OrderStatusEnum.DONE.getValue());
        var list = orderflowMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //根据订单id分组，取insertTime最大的
        return StreamUtils.max(list, Comparator.comparing(OrderFlowDO::getFlowInserttime));

    }

    @Override
    public OrderFlowDO getCloseFlowByOrderId(Long orderId) {
        var list = getCloseFlow(orderId);
        if (CollectionUtils.isEmpty(list)) {
            LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderFlowDO::getFlowOrderid, orderId).eq(OrderFlowDO::getFlowOperation, 12).eq(OrderFlowDO::getFlowClosemark, 6);
            var closeMarkList = orderflowMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(closeMarkList)) {
                return null;
            }
            return StreamUtils.min(closeMarkList, Comparator.comparing(OrderFlowDO::getFlowId));
        }
        //根据订单id分组，取insertTime最大的
        return StreamUtils.max(list, Comparator.comparing(OrderFlowDO::getFlowInserttime));
    }

    private List<OrderFlowDO> getCloseFlow(Long orderId) {
        LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
        //状态为已关闭
        wrapper.eq(OrderFlowDO::getFlowOrderid, orderId).eq(OrderFlowDO::getFlowDststatus, OrderStatusEnum.CLOSE.getValue());
        var list = orderflowMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }


    @Override
    public OrderFlowDO getDispatchFlowByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
        //操作类型为调度
        wrapper.eq(OrderFlowDO::getFlowOrderid, orderId).eq(OrderFlowDO::getFlowOperation, 11);
        var list = orderflowMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //根据订单id分组，取insertTime最大的
        return StreamUtils.max(list, Comparator.comparing(OrderFlowDO::getFlowInserttime));
    }

    @Override
    public OrderFlowDO getRefuseReasonByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
        //状态为已关闭
        wrapper.eq(OrderFlowDO::getFlowOrderid, orderId).eq(OrderFlowDO::getFlowDststatus, OrderStatusEnum.CLOSE.getValue()).eq(OrderFlowDO::getFlowClosemark, 0);
        var list = orderflowMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //根据订单id分组，取insertTime最大的
        return StreamUtils.max(list, Comparator.comparing(OrderFlowDO::getFlowInserttime));
    }

    @Override
    public OrderFlowDO getCloseIngFlowByOrderId(Long orderId, Integer closeMark) {
        LambdaQueryWrapper<OrderFlowDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderFlowDO::getFlowOrderid, orderId).eq(OrderFlowDO::getFlowOperation, 12).eq(OrderFlowDO::getFlowClosemark, closeMark);
        var closeMarkList = orderflowMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(closeMarkList)) {
            return null;
        }
        return StreamUtils.max(closeMarkList, Comparator.comparing(OrderFlowDO::getFlowId));
    }
}
